home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /**** My favourite IO routines ****/
- /************************************************************************/
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "t_cio.h"
-
- #define T_CIOLEN 256
-
- /**** Standard-error subroutine ****/
- /**** exits with errnum ****/
- /**** premsg: msg to show first, errnum: ****/
- /* 0: regular abort */
- /* 1: memory allocation error */
- /* 2: io error */
- /* 3: number out of range */
- /* 4: buffer overflow */
- /* 5: syntax error */
- void t_error(char *premsg,int errnum)
- {
- fprintf(stderr,"\n");
- if(*premsg) fprintf(stderr,"**** %s ****\n\n",premsg);
- fprintf(stderr,"Program terminated - ");
- switch(errnum)
- {
- case 0: fprintf(stderr,"OK.");
- break;
- case 1: fprintf(stderr,"Memory allocation error.");
- break;
- case 2: fprintf(stderr,"In-Out error.");
- break;
- case 3: fprintf(stderr,"Number out of range.");
- break;
- case 4: fprintf(stderr,"Buffer overflow.");
- break;
- case 5: fprintf(stderr,"Syntax error.");
- break;
- default: fprintf(stderr,"Abort.");
- break;
- }
-
- fprintf(stderr,"\n\n");
- exit(errnum);
- return;
- }
-
- static char t_path[T_CIOLEN]={"."};
-
- /* get filename ************************************************/
- char *t_getfnam(void)
- {
- char tmpnam[T_CIOLEN];
- char *s;
-
- printf("Current directory path is: \"%s\"\n",t_path);
- printf("Enter new directory path or press <RETURN>: ");
- if(!(int)gets(tmpnam)) t_error("Can't get input line.",2);
- if(sscanf(tmpnam,"*%s")!=EOF) strcpy(t_path,tmpnam);
- s=t_path+strlen(t_path)+1;
- printf("Enter file name: ");
- while(fflush(stdin),!scanf("%s",s));
- if((int)(s+strlen(s)-tmpnam)>=T_CIOLEN) t_error("Path too long.",4);
- return(s);
- }
-
- /**** opens file, if name==NULL -> asks for file name ****/
- /**** t_path must be set ****/
- /**** if using UNIX etc, change backslashes in function body ****/
- FILE *t_fopen(char *name, char *mode)
- {
- char *s;
- FILE *fep;
-
- s=t_path;
- if(name==(char *)NULL) { t_getfnam(); s+=strlen(t_path); }
- else strcpy((s+=strlen(t_path))+1,name);
-
- *s='\\';
- if((fep=fopen(t_path,mode))==(FILE *)NULL)
- {
- strcat(t_path," could not be opened for ");
- strcat(t_path,mode);
- t_error(t_path,2);
- }
- *s=(char)0;
- return(fep);
- }
-
- /**** Menus ****/
- /**** menu[]={"first item","second item",""} ****/
- /**** returns number of selected item ****/
- int ar_menu(char *menu[])
- {
- int i,c,p,anz;
-
- for(i=0;*menu[i] && i<26;i++)
- {
- printf("%c: %s\n",(char)i+'a',menu[i]);
- }
- anz=i;
- printf("Select: ");
- do
- {
- c=getchar(); c=tolower(c); p=c-'a';
- }
- while(!islower(c) && p<anz && p>=0);
- printf("%s\n\n\n",menu[p]);
- return(p);
- }
-
-
-
-